package com.gxuwz.leaveSystem.controller;


import com.gxuwz.leaveSystem.entity.User;
import com.gxuwz.leaveSystem.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//定义controller
//2.1使用@Controller定义bean
@RestController
public class UserController {
    //加载配置文件
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserService userService = (UserService) applicationContext.getBean("userService");
    //2.2设置当前操作的访问路径


    /**
     * 添加用户
     * 无论成功都返回到列表页面
     * @param user
     * @return
     */

    @RequestMapping("/user_add")
    public ModelAndView add( User user){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/user_list");
        try {
            int num = userService.addUser(user);
            if (num == 1) {
                System.out.println("添加成功！");
                return mv;
            }else {
                System.out.println("添加失败！");
                return mv;
            }

        }catch (Exception e){
            System.out.println("服务器异常");
        }
        return mv;
    }


    /**
     * 编辑用户
     * @param user
     * @return
     */
    @RequestMapping( "/user_edit")
    @ResponseBody
    public ModelAndView updateUser(User user) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/user_list");
        try {
            int num = userService.updateUser(user);
            System.out.println(user);
            if (num==1){
                System.out.println("更新成功！");
                return mv;
            }else {
                System.out.println("更新失败！");
                return mv;
            }
        } catch (Exception e) {
            // 捕获其他异常
            System.out.println("服务器异常");
        }
        return mv;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @RequestMapping("/user_delete")
    public ModelAndView deleteUser(@RequestParam("userid") String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/user_list");
        try {
            int num = userService.deleteUser(id);
            if (num==1){
                System.out.println("删除成功！");
                return mv;
            }else {
                System.out.println("删除失败！");
                return mv;
            }
        } catch (Exception e) {
            // 捕获其他异常
            System.out.println("服务器异常");
        }
        return mv;
    }

    /**
     * 查询所有用户
     * 无论是否成功都跳到列表页面
     * @param
     * @return
     */
    @RequestMapping("/user_list")
    @ResponseBody//需要坐标依赖才能将数据以json形
    public ModelAndView findAllUser() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/user_list");
        try {
            List<User> users = userService.findAllUser();
            mv.addObject("users",users);
            return mv;
        }catch (Exception e){
            System.out.println("服务器异常");
        }
        return mv;
    }
    /**
     * 定向到添加页面
     * 返回预定的数据
     * @return
     */
    @RequestMapping("/user_addView")
    public ModelAndView addVIew(){
        ModelAndView mv = new ModelAndView();
        User user = new User("admin","mazhiju","123456","13347509284","admin");
        mv.addObject("user",user);
        mv.setViewName("user/user_add");
        return mv;
    }

    /**
     * 定向到编辑页面
     * 从数据库中调取数据
     * 输入id找不到返回error
     * @param id
     * @return
     */
    @RequestMapping("/user_editView")
    public ModelAndView editView(@RequestParam("id") String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/user_edit");
        User user;
        try {
           user = userService.findUserByid(id);
           mv.addObject("user",user);
           return mv;
        }catch (Exception e){
            System.out.println("服务器异常");
            e.printStackTrace();
        }
        return mv;
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */

    @RequestMapping("/user_search")
    @ResponseBody
    public ModelAndView searchUsers(@RequestParam("keywords") String keyword) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("user/user_list");
        try {
            List<User> users = userService.searchUsers(keyword);
            if (keyword!=null&&users!=null){
                mv.addObject("users",users);
                return mv;
            }else {
                System.out.println("查找不到或没有！");
            }
        }catch (Exception e){
            System.out.println("服务器异常");
            return null;
        }
        return mv;
    }


}